package com.abc.robot01.activity.compose



import android.annotation.SuppressLint
import android.os.Bundle
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Tab
import androidx.compose.material3.TabRow
import androidx.compose.material3.TabRowDefaults.tabIndicatorOffset
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.fragment.app.Fragment
import com.abc.robot01.R
import com.abc.robot01.fragment.FirstFragment
import com.abc.robot01.fragment.SecondFragment
import kotlinx.coroutines.launch

class PagerFragment2Activity : AppCompatActivity() {
    val   TAG="PagerActivity"
    fun inject(event:MotionEvent){
        findViewById<ViewGroup>(R.id.compose_view).dispatchTouchEvent(event)
    }

    @SuppressLint("CoroutineCreationDuringComposition")
    @OptIn(ExperimentalFoundationApi::class)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val fragments = listOf(
            FirstFragment(),
            SecondFragment(),
        )

//        setContentView(R.layout.pager)
//        findViewById<ComposeView>(R.id.compose_view).
        setContent {
     val coroutineScope = rememberCoroutineScope()

     val tabs = listOf("Tab 1", "Tab 2")
     // Display 10 items
     val pagerState = rememberPagerState(pageCount = {
         tabs.size
     })

     Column(modifier = Modifier.fillMaxSize(),
         horizontalAlignment = Alignment.CenterHorizontally) {
         TabRow(selectedTabIndex = pagerState.currentPage,
             indicator = { tabPositions ->
                 // Use a transparent Box to hide the default indicator
                 Box(
                     Modifier
                         .tabIndicatorOffset(tabPositions[pagerState.currentPage])
                         .background(Color.Transparent)
                         .height(0.dp)
                 )
             },
             modifier  = Modifier
                 .background(Color.Transparent)
                 .width(160.dp),
             divider ={ }
             ) {
             tabs.forEachIndexed { index, _ ->
                 val selected = pagerState.currentPage == index
                 Tab(
                     selected = pagerState.currentPage == index,
                     onClick = { coroutineScope.launch {  pagerState.scrollToPage(index) }},
                         modifier = Modifier
                             .padding(8.dp)
                             .background( if(selected)  Color.Red else Color.Gray ,
                                 shape = RoundedCornerShape(4.dp)
                             ) // Custom background and shape
                             .size(width = 20.dp, height = 5.dp)
                 )
             }
         }
         HorizontalPager(state = pagerState) { page ->
             val fragmentId = R.id.Fragment03
             AndroidView(
                 modifier = Modifier.fillMaxSize(),
                 factory = { context ->
                     val fragment = fragments[page]
                     val transaction = supportFragmentManager.beginTransaction()
                     val frameLayout = FrameLayout(context).apply {
                         id = fragmentId
                     }
                     transaction.add(frameLayout.id, fragment).commitNow()
                     frameLayout
                 }
             )
         }
     }
 }
    }

//    @Composable
//    fun FragmentContainer(fragment: Fragment) {
//        val fragmentTransaction = supportFragmentManager.beginTransaction()
//        fragmentTransaction.replace(R.id.fragment_container, fragment)
//        fragmentTransaction.commit()
//    }

    companion object{
        var  a: PagerFragment2Activity? = null;

        val fragment1 = FirstFragment();
        val fragment2 = SecondFragment();


    }
}